#include "common.h"
#include "jvm.h"
#include "properties.h"

void start(Common &common) {
    Jvm jvm;
    switch (jvm.start()) {
    case ERR_OK:
        break;
    case ERR_LOAD_JVM_DLL:
#ifdef X64
        common.showMessageBox(true, L"启动失败，请检查 jdk/jre 是否是 64 位的！");
#else
        common.showMessageBox(true, L"启动失败，请检查 jdk/jre 是否是 32 位的！");
#endif
        break;
    case ERR_CREATE_JVM:
        common.showMessageBox(true, L"创建java虚拟机失败！");
        break;
    case ERR_MAIN_CLASS_NOT_FOUND:
        common.showMessageBox(true, L"主类未找到，请检查配置：exe.classPath 和 exe.mainClass！");
        break;
    case ERR_RUN_MAIN_METHOD:
        common.showMessageBox(true, L"运行 main 方法失败！");
        break;
    default:
        common.showMessageBox(true, L"启动时发生了未知错误！");
        break;
    }
}

#ifdef DEBUG
// 控制台程序入口，方便调试
int main(int argc, char *argv[]) {
    Common &common = Common::getInstance();
    try {
        start(common);
    } catch (std::runtime_error &e) {
        std::string msg = e.what();
        common.showMessageBox(true, WString(msg.begin(), msg.end()));
        return 1;
    }
    return 0;
}
#else
// windows 窗口程序的入口函数
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
    Common &common = Common::getInstance();
    try {
        start(common);
    } catch (std::runtime_error &e) {
        std::string msg = e.what();
        common.showMessageBox(true, WString(msg.begin(), msg.end()));
        return 1;
    }
    return 0;
}
#endif